﻿using Common.Entities;
using Common.Enums;
using DataAccess.Repository.Interfaces;
using Infrastructure.Interfaces;

namespace Infrastructure.Services
{
    public class GatewayDetailsService : IGatewayDetailsService
    {
        private readonly IConfigurationManager _configurationManager;
        private readonly ISecurityKeyRepository _securityKeyRepository;
        private readonly IUserAccountRepository _userAccountRepository;
        private readonly IUserEncryptor _userEncryptor;

        public GatewayDetailsService(IConfigurationManager configurationManager, ISecurityKeyRepository securityKeyRepository,
                                     IUserAccountRepository userAccountRepository, IUserEncryptor userEncryptor)
        {
            _configurationManager = configurationManager;
            _securityKeyRepository = securityKeyRepository;
            _userAccountRepository = userAccountRepository;
            _userEncryptor = userEncryptor;
        }

        public GatewayDetails GetRequiredGateway(SecurityKey securityKey, SocialNetworkName socialNetworkName)
        {
            //from db
            var userId = _securityKeyRepository.GetUserIdBySecurityKey(securityKey);
            var accounts = _userAccountRepository.GetUserAccounts(userId);
            var account = _userEncryptor.UnsecureUserAccount(accounts.GetAccountBySocialNetwork(socialNetworkName));

            //frm cfg
            return new GatewayDetails
                       {
                           LoginUrl = _configurationManager.LoginUrl,
                           UserName = account.AccountName,
                           Password = account.AccountPassword,
                           UserNameHtmlElementId = _configurationManager.UserNameHtmlElementId,
                           PasswordHtmlElementId = _configurationManager.PasswordHtmlElementId,
                           SubmitButtonName = _configurationManager.SubmitButtonName,
                           LoginSuccessTitle = _configurationManager.LoginSuccessTitle
                       };
        }
    }
}
